import { createStore } from 'vuex';
import { menus } from '@/store/menus';
import { apps } from '@/store/apps';
import { users } from '@/store/users';
import { SysUser, PFTenant, PFUserInfo } from 'pf-component';

export type RootModule = {};
export default createStore<RootModule>({
  getters: {
    currentAppId: (state: any): string => state.apps.currentAppId,
    currentAppName: (state: any): string | undefined => {
      const id = state.apps.currentAppId;
      const apps = state.apps.apps || [];
      let idx = -1;
      if (id && (idx = apps.findIndex((v: any) => v.id === id)) > -1) {
        return apps[idx].appName;
      }
    },
    currentTenId: (state: any): string => state.users.currentTenId,
    currentTenName: (state: any): string | undefined => {
      const id = state.users.currentTenId;
      const tenants = state.users.pfUserInfo.tenants || [];
      let idx = -1;
      if (id && (idx = tenants.findIndex((v: any) => v.id === id)) > -1) {
        return tenants[idx].name;
      }
    },
    userInfo: (state: any): SysUser => state.users.userInfo,
    pfUserInfo: (state: any): PFUserInfo => state.users.pfUserInfo,
    tenants: (state: any): PFTenant[] => state.users.pfUserInfo?.tenants || [],
    apps: (state: any): any[] => state.apps.apps || []
  },
  mutations: {},
  actions: {},
  modules: {
    menus,
    apps,
    users
  }
});
